#!/usr/bin/python
# pylint: disable-msg = W0142
'''
Created by Colin Gillespie <c.gillespie@ncl.ac.uk>
$LastChangedDate: 2008-03-13 19:50:58 +0000 (Thu, 13 Mar 2008) $

'''

__revision__ = '$Rev: 73 $'


from GlobalVariables import globalVariables
from swiginac import symbol, numeric
import MomentCumulantConversion as MCC

class ClosureVariables(object):

    def __init__(self, mom2cum={}, cum2mom={},
       mom_vars={}, cum_vars={}):
        
        self.mom2cum = mom2cum
        self.cum2mom = cum2mom
        self.mom_vars = mom_vars
        self.cum_vars = cum_vars
        
    def getMoment(self, value, moment=True):
        #print self.mom_vars
        #print 'mom2cum:', self.mom2cum
        #print 'cum2mom:', self.cum2mom                
        
        if moment:
            if self.mom_vars.has_key(value):
                return self.mom_vars[value]
            
            if sum(value) <= globalVariables()['closure_order']:
                self.__convertMoment2Maple(value)
                return self.mom_vars[value]
            else:
                self.mom_vars[value] = MCC.closeMoment(value)
                
                return self.mom_vars[value]
                
        else:
            if not self.cum2mom.has_key(value):
                self.cum2mom[value] = self.__closeCumulant(value)
            return self.cum2mom[value]
    
    def getCumulant(self, value, moment=True):

        if not moment:
            if sum(value) > globalVariables()['closure_order']:
                return numeric(0)
            elif self.cum_vars.has_key(value):
                return self.cum_vars[value]
            else:
                kappa = self.__convertCumulant2Maple(value)
                return self.cum_vars[value]
        else:
            if self.mom2cum.has_key(value):
                return self.mom2cum[value]
            else:            
                kappa = MCC.closeMomentAsCumulant(value)


                self.mom2cum[value] = kappa
                return self.mom2cum[value]
    
    
    def __closeCumulant(self, value):
        
        if sum(value) > globalVariables()['closure_order']:
            return numeric(0)
        else:
            moment = MCC.cumulant2Moment(value)
            self.cum2mom[value] = moment.expand()
            return self.cum2mom[value]
           
            
    def __convertMoment2Maple(self, values):
        if sum(values) == 0:
            self.mom_vars[values] = numeric(1)
            
        else:
            inner = ''
            for v in values:
                inner += str(v)
                mu = 'mu_%s(t)' % inner
            self.mom_vars[values] = symbol(mu)
        return self.mom_vars[values]

    def __convertCumulant2Maple(self, values):
        if sum(values) == 0:
            self.cum_vars[values] = numeric(0)
            
        else:
            inner = ''
            for v in values:
                inner += str(v)
                kappa = 'kappa_%s(t)' % inner
            self.cum_vars[values] = symbol(kappa)
        return self.cum_vars[values]
        
    
    def initialise(self):
        self.mom2cum.clear()
        self.cum2mom.clear() 
        self.mom_vars.clear()
        MomentStar().initialise() 


class MomentStar(object):
    
    def __init__(self, mom_star = {}):
        self.mom_star = mom_star

    def initialise(self):
        self.mom_star.clear()
    
    def get(self, tup):
        if self.mom_star.has_key(tup):
            return self.mom_star[tup]
        else:
            self.mom_star[tup] = MCC.getMomentStar(tup)
            return self.mom_star[tup]
    
    def put(self, tup, value):
        if not self.mom_star.has_key(tup):
            self.mom_star[tup] = value
        return True




if __name__ == '__main__':
    
    #print cumulant2Moment((0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0))
 #   print cumulant2Moment((0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1))
    
    print ClosureVariables().getMoment((3,)).expand()
























